﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;

    public class Transfer : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnConfirm;
        protected Button btnCopy;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected DropDownList ddlTransferType;
        protected HtmlForm F5061;
        protected GridView gvTransferDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnTransferID;
        protected HtmlInputHidden hidCheck;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected HtmlImage imgTime;
        protected Label lblFromOwnerID;
        protected Label lblMsg;
        protected Label lblStatus;
        protected Label lblToOwnerID;
        protected Label lblTransfer;
        protected Label lblTransferDate;
        protected Label lblTransferID;
        protected Label lblTransferType;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtFromOwnerID;
        protected TextBox txtStatus;
        protected TextBox txtToOwnerID;
        protected TextBox txtTransferDate;
        protected TextBox txtTransferID;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTransferDate.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_001");
                this.lblMsg.Visible = true;
            }
            else if (string.IsNullOrEmpty(this.txtFromOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_002");
                this.lblMsg.Visible = true;
            }
            else if (string.IsNullOrEmpty(this.txtToOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_003");
                this.lblMsg.Visible = true;
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnTransferID.Value))
                {
                    this.InsertTransferInfo();
                }
                base.Response.Redirect("TransferDetail.aspx?ID=" + base.UrlEncode(this.hdnTransferID.Value) + "&SeqNO=" + base.UrlEncode(this.hdnLineID.Value) + "&from=" + base.UrlEncode(this.hdnFrom.Value) + "&FOwnerID=" + base.UrlEncode(this.txtFromOwnerID.Text) + "&TOwnerID=" + base.UrlEncode(this.txtToOwnerID.Text), true);
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.gvTransferDetail.Rows.Count > 0)
                {
                    List<string> transferID = new List<string>();
                    List<string> lineID = new List<string>();
                    for (int i = 0; i < this.gvTransferDetail.Rows.Count; i++)
                    {
                        if (this.gvTransferDetail.DataKeys[i]["STATUS"].ToString().ToUpper() == "NEW")
                        {
                            transferID.Add(this.gvTransferDetail.DataKeys[i]["TransferID"].ToString());
                            lineID.Add(this.gvTransferDetail.DataKeys[i]["LineID"].ToString());
                        }
                    }
                    if ((lineID == null) || (lineID.Count == 0))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F5300_006");
                    }
                    else
                    {
                        string temp = InventoryMgtServiceClientProxy.GetInstance().ExecuteInvTransfer(transferID[0], lineID, base.UserInfo.UserID);
                        this.lblMsg.Text = base.GetGlobalMsgResource("F5300_007") + this.txtTransferID.Text.Trim() + base.GetGlobalMsgResource("F5300_008") + temp + base.GetGlobalMsgResource("F5300_009");
                        this.SetTransferInfo2Form();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_010") + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtTransferID.Text = "";
            this.txtTransferDate.Text = DateTime.Now.ToShortDateString();
            base.DataEntityViewState = null;
            this.lblMsg.Text = base.GetGlobalMsgResource("F5300_011");
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> transferIDs = new List<string>();
                List<string> lineIDs = new List<string>();
                for (int i = 0; i < this.gvTransferDetail.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvTransferDetail.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        if (this.gvTransferDetail.DataKeys[i]["STATUS"].ToString().ToUpper() != "NEW")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F5300_004");
                            return;
                        }
                        transferIDs.Add(this.gvTransferDetail.DataKeys[i]["TransferID"].ToString());
                        lineIDs.Add(this.gvTransferDetail.DataKeys[i]["LineID"].ToString());
                    }
                }
                if ((transferIDs.Count > 0) && (lineIDs.Count > 0))
                {
                    this.lblMsg.Text = InventoryMgtServiceClientProxy.GetInstance().DeteleTransferDetail(transferIDs, lineIDs) + base.GetGlobalMsgResource("C006");
                    this.SetTransferInfo2Form();
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5300_005");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C012") + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("TransferQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("TransferList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTransferDate.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_001");
                this.lblMsg.Visible = true;
            }
            else if (string.IsNullOrEmpty(this.txtFromOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_002");
                this.lblMsg.Visible = true;
            }
            else if (string.IsNullOrEmpty(this.txtToOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F5300_003");
                this.lblMsg.Visible = true;
            }
            else
            {
                try
                {
                    if (string.IsNullOrEmpty(this.hdnFlag.Value))
                    {
                        this.InsertTransferInfo();
                        this.hdnFlag.Value = "1";
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                    this.lblMsg.Visible = true;
                }
            }
        }

        protected string FormatTransferUrl(string url, string transferID, string LineID, string FromOwnerID, string ToOwnerID)
        {
            return string.Format(url, new object[] { transferID, LineID, FromOwnerID, ToOwnerID });
        }

        protected void gvTransferDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5061", this.gvTransferDetail);
        }

        protected void gvTransferDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int num = e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = num.ToString();
                e.Row.Cells[0].Text = num.ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvTransferDetail_SelectedIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.PageQueryEntitySession as TransferInfo).TransferDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvTransferDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                TransferInfo transferInfo = base.PageQueryEntitySession as TransferInfo;
                DataView dv = CADataConverter.ToDataTable(transferInfo.TransferDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvTransferDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertTransferInfo()
        {
            try
            {
                string transferID = string.Empty;
                TransferInfo transferInfo = new TransferInfo {
                    Status = this.txtStatus.Text.Trim(),
                    FromOwnerID = this.txtFromOwnerID.Text.Trim(),
                    ToOwnerID = this.txtToOwnerID.Text.Trim(),
                    TransferDate = DateTime.Parse(this.txtTransferDate.Text.Trim()),
                    TransferType = this.ddlTransferType.SelectedValue,
                    GenIsInCharges = "N",
                    GenOtCharges = "N",
                    IsActive = "Y",
                    CreatedBy = base.UserInfo.UserID
                };
                transferID = InventoryMgtServiceClientProxy.GetInstance().InsertTransferInfo(transferInfo);
                if (!string.IsNullOrEmpty(transferID))
                {
                    this.hdnTransferID.Value = transferID;
                    this.lblMsg.Text = transferID + base.GetGlobalMsgResource("C004");
                    this.lblMsg.Visible = true;
                    this.txtTransferID.Text = transferID;
                    this.hdnLineID.Value = "10";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }

        private void LoadData()
        {
            this.txtStatus.Text = "New";
            this.txtTransferDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            base.BindData2DDLBySysCode(this.ddlTransferType, "TransferType");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnTransferID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetTransferInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + base.Request.QueryString["detail"].ToString() + ",2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.btnCopy.Visible = false;
        }

        private void SetTransferInfo2Form()
        {
            try
            {
                string transferID = this.hdnTransferID.Value.ToString();
                TransferInfo transferInfo = InventoryMgtServiceClientProxy.GetInstance().GetTransferInfoByID(transferID);
                if (transferInfo != null)
                {
                    base.PageQueryEntitySession = transferInfo;
                    this.txtTransferID.Text = transferInfo.TransferID;
                    this.txtStatus.Text = transferInfo.Status.ToString();
                    this.txtFromOwnerID.Text = transferInfo.FromOwnerID;
                    this.txtFromOwnerID.Enabled = false;
                    this.img1.Visible = false;
                    this.img2.Visible = false;
                    this.txtToOwnerID.Text = transferInfo.ToOwnerID;
                    this.txtToOwnerID.Enabled = false;
                    this.txtTransferDate.Text = base.GetPreferenceShortDate(transferInfo.TransferDate);
                    this.txtTransferDate.Enabled = false;
                    this.imgTime.Visible = false;
                    this.ddlTransferType.SelectedValue = transferInfo.TransferType;
                    this.ddlTransferType.Enabled = false;
                    if ((transferInfo.TransferDetailInfos != null) && (transferInfo.TransferDetailInfos.Count > 0))
                    {
                        this.hdnLineID.Value = (transferInfo.TransferDetailInfos[transferInfo.TransferDetailInfos.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        this.hdnLineID.Value = "10";
                    }
                    base.BindData2GridView(this.gvTransferDetail, transferInfo.TransferDetailInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

